/*
 *  touchHell.m
 *  touchhell
 *
 *  Created by omega on 08/12/22.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#import "touchHell.h"
#import "Texture2D.h"
#import "EAGLView.h"
#import "SoundEngine.h"

UInt32 _sounds[HELL_SOUND_MAX];
Boolean hell_isBgmLoaded = NO;

// load texture
void Hell_loadTextureFromFile( int tex_idx , char* filename ) {
	NSString* filestr;
	filestr = [NSString stringWithCString:filename];
	[_textures[ tex_idx ] dealloc];
	_textures[ tex_idx ] = [[Texture2D alloc] initWithImage:[UIImage imageNamed:filestr]];
}

// draw font
void Hell_drawFontWithTexture( char* str , TextureFontParam fontparam ){
	[_textures[ fontparam.tex_idx ] drawFontWithCString:str fontParam:fontparam R:1 G:1 B:1 A:1];
}
void Hell_drawFontWithTextureColor( char* str, TextureFontParam fontparam , int r , int g , int b , int a ){
	[_textures[ fontparam.tex_idx ] drawFontWithCString:str fontParam:fontparam  R:(r/255.0f) G:(g/255.0f) B:(b/255.0f) A:(a/255.0f)];
}

// draw tecture
void Hell_drawTextureColor( int tex_idx , float tx , float ty , float tw , float th , int r , int g , int b , int a){
	[_textures[ tex_idx ] drawClipRect:CGRectMake(tx,ty,tw,th) DestPoint:CGPointZero R:(r/255.0f) G:(g/255.0f) B:(b/255.0f) A:(a/255.0f)];
}
void Hell_drawTexture( int tex_id , float tx , float ty , float tw, float th ){
	Hell_drawTextureColor(tex_id , tx, ty, tw, th , 255,255,255,255);
}


void Hell_bindTexture( int tex_idx ){
	[_textures[ tex_idx ] bindTexture];
}
void Hell_setAlphaBlendMode( Hell_AlphaBlendMode mode ){
	switch( mode ){
		case HELL_ALPHABLEND_NORMAL:
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			break;
		case HELL_ALPHABLEND_ADD:
			glBlendFunc(GL_SRC_ALPHA, GL_ONE);
			break;
		default:
			break;
	}
}

float Hell_getAccelerometerX() {return _accelerate.x;}
float Hell_getAccelerometerY() {return _accelerate.y;}
float Hell_getAccelerometerZ() {return _accelerate.z;}

// SoundEngine wrapper
void Hell_loadSoundEffectFromFile( int snd_idx , char* filename ){
	NSBundle* bundle = [NSBundle mainBundle];
	NSMutableString* path = [NSMutableString stringWithString:[bundle resourcePath]];
	NSString* name = [[NSString alloc] initWithCString:filename];
	[path appendString:@"/"];
	[path appendString:name];
	NSLog( @"SoundEngine_loadEffect:%d" , SoundEngine_LoadEffect([path UTF8String] , &_sounds[snd_idx]));
	[name release];
}

void Hell_playSoundEffect( int snd_idx ){	
	SoundEngine_StartEffect( _sounds[snd_idx] );
}

void Hell_loadBgm( char* filename ){
	NSBundle* bundle = [NSBundle mainBundle];
	NSMutableString* path = [NSMutableString stringWithString:[bundle resourcePath]];
	NSString* name = [[NSString alloc] initWithCString:filename];
	[path appendString:@"/"];
	[path appendString:name];
	NSLog( @"SoundEngine_loadBackgroundMusicTrack:%d" , SoundEngine_LoadBackgroundMusicTrack([path UTF8String] , true , true ));
	[name release];
}

void Hell_playBgm(){
	SoundEngine_StartBackgroundMusic();
}

void Hell_stopBgm(){
	SoundEngine_StopBackgroundMusic( false );
}

void Hell_setBgmVolume( float volume ){
	SoundEngine_SetBackgroundMusicVolume( volume );
}

void Hell_logwrite( char* str ){
	NSString *nsstr = [NSString stringWithCString:str];
	NSLog( nsstr );
}


// NSUserDefaults
bool Hell_saveBytes( char* str , void* bytes , size_t size ){
	NSData *data = [NSData dataWithBytes:bytes length:(NSInteger)size];
	NSString *key = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
	[[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
	
	NSLog( @"saveBytes key:%@ size:%d" , key , size );
	
	return true;
}

bool Hell_loadBytes( char* str , void* bytes , size_t size ){
	NSString *key = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
	NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:key];

	if( data == nil ){
		NSLog( @"loadBytes key:%@ size:%d / FAIL" , key , size );
		return false;
	}

	NSLog( @"loadBytes key:%@ size:%d / ok" , key , size );
	[data getBytes:bytes length:(NSUInteger)size];
	return true;
}

bool Hell_synchronize(){
	bool result = [[NSUserDefaults standardUserDefaults] synchronize];
	if( result ){
		NSLog(@"NSUserDefaults synchronized / ok" );
	}else{
		NSLog(@"NSUserDefaults synchronized / FAIL" );
	}
	return result;
}